\input texinfo
@documentencoding UTF-8

@macro textsuperscript{text}
@iftex
@textsuperscript{\text\}
@end iftex
@ifnottex
^@{\text\@}
@end ifnottex
@end macro

@ifnottex
@paragraphindent 0
@end ifnottex
@titlepage
@title EHCI debugging on the BeagleBone Black
@end titlepage

@node Top
@top EHCI debugging on the BeagleBone Black

@menu
* EHCI debugging on the BeagleBone Black::
* EHCI debugging::
@end menu

@node EHCI debugging on the BeagleBone Black
@chapter EHCI debugging on the BeagleBone Black
@anchor{#ehci-debugging-on-the-beaglebone-black}
@uref{index.html,Back to previous index}

@node EHCI debugging
@chapter EHCI debugging
@anchor{#ehci-debugging}
@enumerate 
@item
@ref{#FindUSBportonthetargetthatsupportsEHCIdebug,Find USB port on the target that supports EHCI debug}
@item
@ref{#InitialsetupofBBBtoactasEHCIdebugdongle,Initial setup of BBB to act as EHCI debug dongle}
@item
@ref{#PatchBBBsgdbgpmoduleoptionalbuthighlyrecommended,Patch BBB's @code{g_dbgp} module (optional, but highly recommended)}
@item
@ref{#ConfigurelibrebootwithEHCIdebug,Configure libreboot with EHCI debug}
@enumerate 
@item
@ref{#SelectingHCDIndexandUSBDebugport,Selecting @code{HCD Index} and @code{USB Debug port}}
@end enumerate

@item
@ref{#Howtogetthedebuglogs,How to get the debug logs}
@item
@ref{#EnebleEHCIDebugonthetargetskerneloptionalrecommended,Eneble EHCI Debug on the target's kernel (optional, recommended)}
@item
@ref{#References,References}
@end enumerate

If your computer does not boot after installing libreboot, it is very useful to get debug logs from it, from the payload (grub) and/or the kernel (if gets to there). All of them stream debug logs on the available serial (RS-232) by default. However, most of todays laptops lack RS-232 port. The other option is to stream the logs to USB EHCI debug port.

This section explains step-by-step how to setup BBB as a “USB EHCI debug dongle” and configure libreboot and the linux kernel to stream logs to it (TODO: grub).

I will refer to three computers:

@itemize
@item
@strong{host} - this is the computer you use, have tools, compiler, Internet, etc
@item
@strong{BBB} - Beaglebone Black (rev. B or higher, i use rev. C)
@item
@strong{target} - the computer you are trying to install liberboot
@end itemize

@node Find USB port on the target that supports EHCI debug
@subsection Find USB port on the target that supports EHCI debug
@anchor{#find-usb-port-on-the-target-that-supports-ehci-debug}
Not all USB controllers support EHCI debug (see: @uref{http://www.coreboot.org/EHCI_Debug_Port#Hardware_capability,EHCI Debug Port} ). Even more, if a USB controller supports EHCI debug, it is available only @strong{on a single port} that might or might not be exposed externally.

@itemize
@item
You need running OS (GNU/Linux) on your target for this step (If you've flashed libreboot and it does not boot, you have to flush back the stock bios)
@item
You need USB memory stick (the data on it will not be touched).
@item
The EHCI debugging can not be done through external hub, BBB must be connected directly to the debug port of the controller (so, no hubs)
@end itemize

@itemize
@item
Download@textsuperscript{@ref{#___fn1,1}} @uref{http://www.coreboot.org/pipermail/coreboot/attachments/20080909/ae11c291/attachment.sh,this} shell script.
@end itemize

@enumerate 
@item
Plug the usb stick in the first available usb port
@item
Run the script, you will get output similar to following:
@item
The buses the support debug are Bus 3 (0000:00:1a.0) on Port 1 and Bus 4 (0000:00:1d.0) on port 2. Your usb stick is plugged on Bus 1, Port 3
@item
Repeat the steps, plugging the USB stick in the next available port
@item
Go through all available ports and remember(write down) those for which bus/port of the usb stick matches one of the bus/port that support debug (bold).
@end enumerate

Remember (write down) for each port (external plug) you found that supports debug: @strong{PCI device id, the bus id, the port number, and the physical location of the usb plug.}

If you do not find a match, you can not get debug over EHCI. Sorry.

@textsuperscript{1} The guys from coreboot were talking about including the script in coreboot distribution (check the status).

@node Initial setup of BBB to act as EHCI debug dongle
@subsection Initial setup of BBB to act as EHCI debug dongle
@anchor{#initial-setup-of-bbb-to-act-as-ehci-debug-dongle}
BBB must be powered with a barrel power connector since the mini-B USB plug will be used for the EHCI debug stream. So you will need:

@itemize
@item
power supply (5V, 2A(10W) is sufficient).
@item
an extra usb cable: A to mini-B
@end itemize

(On BBB) The linux kernel includes module (g_dbgp that enables one of the usb ports on a computer to behave as EHCI debug dongle. Make sure you have this module available on your BBB (Debian 7.8 that comes with BBB should have it), if not, you should compile it yourself (see next section):

@verbatim
ls /lib/modules/3.8.13-bone70/kernel/drivers/usb/gadget/g_dbgp.ko
@end verbatim

Unload all other g_* modules:

@verbatim
# lsmod
# rmmod g_multi
...
@end verbatim

Then load g_dbgp :

@verbatim
# modprobe g_dbgp
# lsmod # should show that g_dbgp is loaded, and no other g_*
@end verbatim

Plug the mini-B side of the USB cable in your BBB and the A side in your target. Then one of the usb devices on your target (with lsusb ) should be:

@verbatim
Bus 001 Device 024: ID 0525:c0de Netchip Technology, Inc.
@end verbatim

If you see the device on the target, you are good to continue to the next step.

@node Patch BBB's g_dbgp module optional but highly recommended
@subsection Patch BBB's g_dbgp module (optional, but highly recommended)
@anchor{#patch-bbbs-g_dbgp-module-optional-but-highly-recommended}
For the reasons why you need this, see: @uref{http://www.coreboot.org/EHCI_Gadget_Debug,EHCI Gadget Debug}.@*Make sure that you have cross compiling environment for arm-linux-gnueabihf setup on your @emph{host}.

@itemize
@item
On BBB: uname -r - this will give you version number like 3.8.13-bone70 (I will refer to this as: $mav.$miv-$lv: where mav=3.8, miv=13, lv=bone70
@item
Get the BBB kernel ready on your host for cross-compiling:
@end itemize

@verbatim
$ cd $work_dir
$ git clone https://github.com/beagleboard/kernel.git
$ cd kernel
$ git checkout $mav (see above)
$ ./patch.sh
$ wget http://arago-project.org/git/projects/?p=am33x-cm3.git\;a=blob_plain\;f=bin/am335x-pm-firmware.bin\;hb=HEAD -O kernel/firmware/am335x-pm-firmware.bin
$ cp configs/beaglebone kernel/arch/arm/configs/beaglebone_defconfig
@end verbatim

@itemize
@item
Download the patch from @uref{http://www.coreboot.org/images/8/88/Ehci-debug-gadget-patches.tar.gz,here}
@item
tar -xf Ehci-debug-gadget-patches.tar.gz (will create dir: usbdebug-gadget)
@item
Note that there are two patches (patch_1 and patch_2) for each of the two different version of the kernel (3.8 and 3.10). I will use 3.8. (If using kernel 3.12 patch_1 is not needed)
@item
cd kernel (note that this is one more level: you should be in $work_dir/kernel/kernel)
@item
Apply the patches:
@end itemize

@verbatim
git apply ../usbdebug-gadget/v3.8-debug-gadget/0001-usb-dbgp-gadget-Fix-re-connecting-after-USB-disconne.patch
git apply ../usbdebug-gadget/v3.8-debug-gadget/0002-usb-serial-gadget-no-TTY-hangup-on-USB-disconnect-WI.patch
;
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- beaglebone_defconfig -j4@
@end verbatim

@itemize
@item
You should also apply the linux-libre @emph{deblob} script to turn it into linux-libre (deletes all the blobs from the linux kernel). @uref{http://www.fsfla.org/ikiwiki/selibre/linux-libre/,fsfla website} - see @uref{http://www.fsfla.org/svn/fsfla/software/linux-libre/scripts/,scripts}.
@item
Get your current BBB kernel config (from: /boot/config-<ver>) and copy it to your host as $work_dir/kernel/kernel/.config
@item
Set proper version number:
@itemize
@item
On your host, edit $work_dir/kernel/kernel/.config (the one you've just copied from BBB), find the line CONFIG_LOCALVERSION="<something or empty>" and change it to CONFIG_LOCALVERSION="-$lv", so it will look something like: CONFIG_LOCALVERSION="-bone70"
@end itemize

@item
Also, make sure that: CONFIG_USB_G_DBGP=m (If not, make menuconfig, and set @@Device Drivers-> USB Support -> USB Gadget Support -> EHCI Debug Device Gadget=m
@item
Build the module:
@end itemize

@verbatim
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4 (is it possoble to build only the gadget modules)
$ mkdir ../tmp && make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../tmp modules_install
@end verbatim

@itemize
@item
on BBB, backup /lib/modules/3.8.13-bone70/kernel/drivers/usb/gadget (i.e. mv /lib/modules/3.8.13-bone70/kernel/drivers/usb/gadget $HOME)
@item
copy the freshly compiled usb/gadget dir to /lib/modules/3.8.13-bone70/kernel/drivers/usb
@item
restart BBB
@item
Remove all g_* modules (rmmod g_<>)
@item
modprobpe g_dbgp
@end itemize

@node Configure libreboot with EHCI debug
@subsection Configure libreboot with EHCI debug
@anchor{#configure-libreboot-with-ehci-debug}
Libreboot(coreboot) should be configured with debug turned on and to push debug messages to the EHCI debug port.@*If you've downloaded the binary distribution, you can check if it is properly configured in the following way:

@itemize
@item
Go to the libreboot dist root directory cd $libreboot_bin
@item
Locate the rom image for your target (I will call it: $img_path)
@item
Running the following command will extract the config in a file ./my_config:
@end itemize

@verbatim
./cbfstool/i686/cbfstool $img_path extract -n config -f ./my_config
@end verbatim

@itemize
@item
Make sure that the following params in the config are set as following:
@end itemize

@verbatim
CONFIG_USBDEBUG=y (Generic Drivers -> USB 2.0 EHCI debug dongle support)
CONFIG_USBDEBUG_IN_ROMSTAGE=y (Generic Drivers -> Enable early (pre-RAM) usbdebug)
CONFIG_USBDEBUG_HCD_INDEX=<HCD Index of usb controller - see below> (Generic Drivers -> Index for EHCI controller to use with usbdebug)
CONFIG_USBDEBUG_DEFAULT_PORT=<USB Debug port - see below> (Generic Drivers -> Default USB port to use as Debug Port)
@end verbatim

The following three are behind radio button in the menu. Only the first one@textsuperscript{@ref{#___fn2,2}} should be = y

@verbatim
USBDEBUG_DONGLE_STD=y                       (Generic Drivers -> Type of dongle (Net20DC or compatible) -> Net20DC or compatible)
CONFIG_USBDEBUG_DONGLE_BEAGLEBONE=n         (Generic Drivers -> Type of dongle (Net20DC or compatible) -> BeagleBone)
CONFIG_USBDEBUG_DONGLE_BEAGLEBONE_BLACK=n   (Generic Drivers -> Type of dongle (Net20DC or compatible) -> BeagleBone Black)
@end verbatim

@textsuperscript{2} The g_dbgp module on BeagleBone Black (Rev. C) reports it self as Net20DC, the other options are for older BB(B) - ver1. This is documented @uref{https://johnlewis.ie/coreboot-ehci-debug-gadget-demonstration/,here} (also tested/verified).

Then:@*

@verbatim
CONFIG_CONSOLE_USB=y (Console -> USB dongle console output)
@end verbatim

Also Debugging ---> Output verbose XYZ ) (@strong{FIXME} somebody verify these):

@verbatim
CONFIG_DEBUG_CBFS=y (Output verbose CBFS debug messages )
CONFIG_HAVE_DEBUG_RAM_SETUP=y (??? What/where is this)
CONFIG_DEBUG_RAM_SETUP=y (Output verbose RAM init debug messages)
CONFIG_DEBUG_SMI=y      (Output verbose SMI debug messages)
CONFIG_DEBUG_ACPI=y     (Output verbose ACPI debug messages )
CONFIG_DEBUG_USBDEBUG=y (Output verbose USB 2.0 EHCI debug dongle messages)
@end verbatim

If some of the above mentioned configuration options are not as specified, you have to configure and compile libreboot yourself. Please refer to the doc(@strong{FIXME: link} about compiling libreboot.
@menu
* Selecting HCD Index and USB Debug port::
@end menu

@node Selecting HCD Index and USB Debug port
@subsubsection Selecting HCD Index and USB Debug port
@anchor{#selecting-hcd-index-and-usb-debug-port}
This applies (and works) only if the USB controller that supports debug (found in the first section) is from Intel.@*If the PCI ID of the port you found in the first section is 0000:00:1a.0 or 0000:00:1d.0 , you are ok. Otherwise you have to try without guarantee that will work.

If the externally exposed port is on a bus with PCI ID == 0000:00:1a.0 then for CONFIG_USBDEBUG_HCD_INDEX choose 2, otherwise choose 0 .

For CONFIG_USBDEBUG_DEFAULT_PORT choose the port from the first section that correspond to the PCI ID

Notes:@*The above is based on the implementation of coreboot/src/southbridge/intel/common/usb_debug.c : pci_ehci_dbg_dev() .@*This is enough as it applies for the supported GM45/G45 Thinkpads. coreboot support some other contollers too, but they are irellevent for libreboot (for now).

@itemize
@item
On T500 (with switchable GPU) the debug ports for both intel controllers is exposed.
@item
On x200t the debug ports for both intel controllers is exposed.
@end itemize

@node How to get the debug logs
@subsection How to get the debug logs
@anchor{#how-to-get-the-debug-logs}
@itemize
@item
Plug the USB cable in the target's debug port (the one you found in step 1) and BBB's mini-B USB
@item
Make sure no other then g_dbgp of the g_* modules is loaded on your BBB
@item
On the BBB:
@end itemize

@verbatim
stty -icrnl -inlcr -F /dev/ttyGS0
cat /dev/ttyGS0
@end verbatim

@itemize
@item
Power on the target with libreboot
@item
You should see debug logs comming on your BBB console
@end itemize

Note that this is not permanent on BBB, if you reboot it, you have to rmmod g_* and modprobe g_dbgp

@node Eneble EHCI Debug on the target's kernel optional recommended
@subsection Eneble EHCI Debug on the target's kernel (optional, recommended)
@anchor{#eneble-ehci-debug-on-the-targets-kernel-optional-recommended}
You have to know how to compile kernel for your target.

@enumerate 
@item
Check if early debugging is already enabled: grep CONFIG_EARLY_PRINTK_DBGP /boot/config-<ver>
@item
If enabled, you do not have to compile the kernel (skip this step). Otherwise, prepare kernel source for your distribution and select (Kernel hacking -> Early printk via EHCI debug port). Compile and install the new kernel.
@item
Edit your grub configuration and add following to the kenel parameters@textsuperscript{@ref{#___fn20,20}}@textsuperscript{@ref{#___fn21,21}}: earlyprintk=dbgp,keep. Also, try: earlyprintk=dbgp<N>,keep where N is the debug port id if the first does not work.
@end enumerate

@node References
@subsection References
@anchor{#references}
@textsuperscript{10} @uref{http://www.coreboot.org/EHCI_Debug_Port,EHCI Debug Port}

@textsuperscript{11} @uref{https://johnlewis.ie/coreboot-ehci-debug-gadget-demonstration/,coreboot EHCI debug gadget demonstration}

@textsuperscript{12} @uref{http://www.coreboot.org/EHCI_Gadget_Debug,EHCI Gadget Debug}

@textsuperscript{13} @uref{http://www.coreboot.org/images/8/88/Ehci-debug-gadget-patches.tar.gz,Ehci-debug-gadget-patches.tar.gz}

@textsuperscript{14} @uref{http://wiki.beyondlogic.org/index.php/BeagleBoneBlack_Building_Kernel,Compiling the BeagleBone Black Kernel}

@textsuperscript{15} http://dumb-looks-free.blogspot.ca/2014/06/beaglebone-black-bbb-compile-kernel.html

@textsuperscript{16} http://dumb-looks-free.blogspot.fr/2014/06/beaglebone-black-bbb-kernal-headers.html

@textsuperscript{17} @uref{http://elinux.org/Building_BBB_Kernel,Building BBB Kernel}

@textsuperscript{18} http://komposter.com.ua/documents/USB-2.0-Debug-Port%28John-Keys%29.pdf

@textsuperscript{19} @uref{http://cs.usfca.edu/~cruse/cs698s10/,Exploring USB at the Hardware/Software Interface}

@textsuperscript{20} https://www.kernel.org/doc/Documentation/x86/earlyprintk.txt

@textsuperscript{21} https://wiki.ubuntu.com/Kernel/Debugging/USBearlyprintk

@strong{TODO}:

@enumerate 
@item
grub does not send messages to EHCI debug. Investigate.
@item
The section “Configure libreboot with EHCI debug” can be skipped/simplified if a common configuration works for all relevant targets is selected as defualt
@item
Patch and compule g_dbgp on BBB instead cross-compile
@item
Find a simple way to send debug messages from targets userland
@end enumerate

Copyright © 2015 Alex David <opdecirkel@@gmail.com>@* Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license can be found at @uref{../resources/licenses/gfdl-1.3.txt,gfdl-1.3.txt}

Updated versions of the license (when available) can be found at @uref{https://www.gnu.org/licenses/licenses.html,https://www.gnu.org/licenses/licenses.html}

UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.

TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.

The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability.

@bye
